{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "index_key = {\n",
    "    'SPTSX': 'Canada',\n",
    "    'FCHI': 'France',\n",
    "    'KS11': 'South Korea',\n",
    "    'SPX': 'United States',\n",
    "    'GDAXI': 'Germany',\n",
    "    'N225':'Japan',\n",
    "    'AS51':'Australia',\n",
    "    'SENSEX': 'India',\n",
    "    'IBOVESPA':'Brazil',\n",
    "    'RTS':'Russia',\n",
    "    'TWII':'Taiwan',\n",
    "    'HSI':'Hong Kong'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "pro = ts.pro_api('')\n",
    "#获取全球指数\n",
    "df = pro.index_globaL(trade_date='20200324', fields='pct_chg,ts_code')\n",
    "df['country'] = df['ts_code'].map(index_key)\n",
    "df = df.dropna(subset=['country'])\n",
    "df = df[['ts_code', 'pct_chg', 'country']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#获取上证指数，并合并\n",
    "sh = pro.index_daily(ts_code='000001.SH', fields='ts_code,pct_chg,ts_code', trade_date='20200220')\n",
    "sh['country'] = 'China'\n",
    "data = df.append(sh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>country</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HSI</td>\n",
       "      <td>4.4587</td>\n",
       "      <td>Hong Kong</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>N225</td>\n",
       "      <td>7.1328</td>\n",
       "      <td>Japan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>KS11</td>\n",
       "      <td>8.6012</td>\n",
       "      <td>South Korea</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>TWII</td>\n",
       "      <td>4.4498</td>\n",
       "      <td>Taiwan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>SPX</td>\n",
       "      <td>9.3828</td>\n",
       "      <td>United States</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>FCHI</td>\n",
       "      <td>8.3895</td>\n",
       "      <td>France</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>GDAXI</td>\n",
       "      <td>10.9759</td>\n",
       "      <td>Germany</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>AS51</td>\n",
       "      <td>4.1711</td>\n",
       "      <td>Australia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>SENSEX</td>\n",
       "      <td>2.6665</td>\n",
       "      <td>India</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>IBOVESPA</td>\n",
       "      <td>9.6897</td>\n",
       "      <td>Brazil</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>RTS</td>\n",
       "      <td>9.2263</td>\n",
       "      <td>Russia</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>SPTSX</td>\n",
       "      <td>11.9385</td>\n",
       "      <td>Canada</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000001.SH</td>\n",
       "      <td>1.8402</td>\n",
       "      <td>China</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      ts_code  pct_chg        country\n",
       "0         HSI   4.4587      Hong Kong\n",
       "1        N225   7.1328          Japan\n",
       "3        KS11   8.6012    South Korea\n",
       "4        TWII   4.4498         Taiwan\n",
       "6         SPX   9.3828  United States\n",
       "9        FCHI   8.3895         France\n",
       "10      GDAXI  10.9759        Germany\n",
       "11       AS51   4.1711      Australia\n",
       "12     SENSEX   2.6665          India\n",
       "13   IBOVESPA   9.6897         Brazil\n",
       "14        RTS   9.2263         Russia\n",
       "15      SPTSX  11.9385         Canada\n",
       "0   000001.SH   1.8402          China"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min', 'world':'https://assets.pyecharts.org/assets/maps/world'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"1df3d814f776417faec44382cbbe8138\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts', 'world'], function(echarts) {\n",
       "                var chart_1df3d814f776417faec44382cbbe8138 = echarts.init(\n",
       "                    document.getElementById('1df3d814f776417faec44382cbbe8138'), 'white', {renderer: 'canvas'});\n",
       "                var option_1df3d814f776417faec44382cbbe8138 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"map\",\n",
       "            \"label\": {\n",
       "                \"show\": false,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"mapType\": \"world\",\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"Hong Kong\",\n",
       "                    \"value\": 4.4587\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"Japan\",\n",
       "                    \"value\": 7.1328\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"South Korea\",\n",
       "                    \"value\": 8.6012\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"Taiwan\",\n",
       "                    \"value\": 4.4498\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"United States\",\n",
       "                    \"value\": 9.3828\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"France\",\n",
       "                    \"value\": 8.3895\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"Germany\",\n",
       "                    \"value\": 10.9759\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"Australia\",\n",
       "                    \"value\": 4.1711\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"India\",\n",
       "                    \"value\": 2.6665\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"Brazil\",\n",
       "                    \"value\": 9.6897\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"Russia\",\n",
       "                    \"value\": 9.2263\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"Canada\",\n",
       "                    \"value\": 11.9385\n",
       "                },\n",
       "                {\n",
       "                    \"name\": \"China\",\n",
       "                    \"value\": 1.8402\n",
       "                }\n",
       "            ],\n",
       "            \"roam\": true,\n",
       "            \"zoom\": 1,\n",
       "            \"showLegendSymbol\": false,\n",
       "            \"emphasis\": {},\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\": true\n",
       "            },\n",
       "            \"show\": true\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u56fd\\u9645\\u4e3b\\u8981\\u6307\\u6570\\u8868\\u73b0\"\n",
       "        }\n",
       "    ],\n",
       "    \"visualMap\": {\n",
       "        \"show\": true,\n",
       "        \"type\": \"continuous\",\n",
       "        \"min\": -2,\n",
       "        \"max\": 2,\n",
       "        \"inRange\": {\n",
       "            \"color\": [\n",
       "                \"green\",\n",
       "                \"red\"\n",
       "            ]\n",
       "        },\n",
       "        \"calculable\": true,\n",
       "        \"splitNumber\": 5,\n",
       "        \"orient\": \"vertical\",\n",
       "        \"showLabel\": true\n",
       "    }\n",
       "};\n",
       "                chart_1df3d814f776417faec44382cbbe8138.setOption(option_1df3d814f776417faec44382cbbe8138);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x28339b8fcf8>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def index_global():\n",
    "    c = (\n",
    "        Map()\n",
    "        .add(\"\", \n",
    "             data_pair=[list(z) for z in zip(data['country'].values, data['pct_chg'].values)], \n",
    "             maptype=\"world\",\n",
    "             is_map_symbol_show=False)\n",
    "        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "        .set_global_opts(\n",
    "            title_opts=opts.TitleOpts(title=\"国际主要指数表现\"),\n",
    "            visualmap_opts=opts.VisualMapOpts(max_=2,\n",
    "                                              min_=-2,\n",
    "                                              is_calculable=True,\n",
    "                                             range_color = ['green', 'red']),\n",
    "        )\n",
    "    )\n",
    "    return c\n",
    "index_global().render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
